package com.example.lenovo.contentprovide;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private SQLiteDatabase database ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyOpenHelper helper = new MyOpenHelper(this);
        database = helper.getReadableDatabase();
    }

    public void insert(View view) {
        database.execSQL("insert into info(name,phone) values('王五','13777')");
        database.execSQL("insert into info(name,phone) values('张三','13555')");
        database.execSQL("insert into info(name,phone) values('李四','13666')");
        database.execSQL("insert into info(name,phone) values('赵六','13444')");

    }
    public void delete(View view) {
        database.execSQL("delete from info where name='王五'");
    }
    public void update(View view) {
        database.execSQL("update info set name='赵赵赵' where id = 3");
    }
    public void query(View view) {
        Cursor cursor = database.rawQuery("select * from info",null);
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String phone = cursor.getString(cursor.getColumnIndex("phone"));
            Toast.makeText(getApplicationContext(),"name:" + name + ",phone:" + phone,Toast.LENGTH_SHORT).show();
        }
        cursor.close();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        database.close();
    }
}
